package org.momo.rpc.config;


import lombok.extern.slf4j.Slf4j;
import org.momo.rpc.config.config.RpcConfig;

import java.lang.reflect.Field;
import java.util.Map;


/**
 * RPC应用
 * 相当于holder ,存放了项目全局用到的变量，双检锁实现单例
 */
@Slf4j
public class RpcConfigLoader {
    private static volatile RpcConfig rpcConfig;

    /**
     * 获取配置
     */
    public static RpcConfig getRpcConfig() {
        if (rpcConfig == null) {
            synchronized (RpcConfigLoader.class) {
                if (rpcConfig == null) {
                    init();
                }
            }
        }
        return rpcConfig;
    }

    /**
     * 初始化
     */
    public static void init() {
        try {
//            rpcConfig = initRpcConfig();
            rpcConfig = new RpcConfig();
        } catch (Exception e) {
            // 读取配置文件失败，使用默认配置
            log.error("Load RPC config error, use default config, the error is {}", e.getMessage());
            rpcConfig = new RpcConfig();
        }
        log.info("RPC application init success, config:{}", rpcConfig);
    }

    private static RpcConfig initRpcConfig() throws Exception {
        RpcConfig rpcConfig = new RpcConfig();

        // 读取配置文件
        Map<String, Object> file = YamlLoader.loadConfig("rpc-config.yml");
        if (file == null) {
            throw new RuntimeException("Load RPC config error, the config file does not exist");
        }
        if(file.isEmpty() || !file.containsKey("momo-rpc")) {
            throw new RuntimeException("Load RPC config error, the config file is incorrect");
        }

        Map<String, Object> allConfigMap = (Map<String, Object>) file.get("momo-rpc");

        // provider server config
        Map<String, Object> serverConfig = (Map<String, Object>) allConfigMap.get("server");
        if(serverConfig != null && !serverConfig.isEmpty()) {
            String providerHost = (String) serverConfig.get("host");
            Integer providerPort = (Integer) serverConfig.get("port");
            if(providerHost == null || providerHost.isEmpty()) {
                throw new IllegalArgumentException("provider host is null or empty");
            }
            if(providerPort == null) {
                throw new IllegalArgumentException("provider port is null or empty");
            }
            rpcConfig.setServerHost(providerHost);
            rpcConfig.setServerPort(providerPort);
        }

        // 加载组件
        for(Map.Entry<String, Object> entry : allConfigMap.entrySet()) {
            String configKey = entry.getKey();
            Map<String, Object> config = (Map<String, Object>) entry.getValue();
            if(config == null || config.isEmpty() || !config.containsKey("type")) {
                continue;
            }
            // 用户配置覆盖默认配置
            try {
                Field field = rpcConfig.getClass().getField(configKey);
                field.setAccessible(true);
                field.set(rpcConfig, config.get("type"));
            } catch (NoSuchFieldException exception) {
                log.error("no such field");
                throw new RuntimeException("/");
            }
        }

        // 加载registry配置
        if(allConfigMap.containsKey("registry")) {
            Map<String, Object> registryConfig = (Map<String, Object>) allConfigMap.get("registry");

        }

        return rpcConfig;
    }


}
